package com.ruoyi.web.controller.note;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.note.domain.NoteDaily;
import com.ruoyi.note.service.INoteDailyService;

/**
 * 日常笔记Controller
 * 
 * @author ruoyi
 * @date 2025-01-22
 */
@RestController
@RequestMapping("/note/daily")
public class NoteDailyController extends BaseController
{
    @Autowired
    private INoteDailyService noteDailyService;

    /**
     * 查询日常笔记列表
     */
    @PreAuthorize("@ss.hasPermi('note:daily:list')")
    @GetMapping("/list")
    public TableDataInfo list(NoteDaily noteDaily)
    {
        startPage();
        List<NoteDaily> list = noteDailyService.selectNoteDailyList(noteDaily);
        return getDataTable(list);
    }

    /**
     * 导出日常笔记列表
     */
    @PreAuthorize("@ss.hasPermi('note:daily:export')")
    @Log(title = "日常笔记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NoteDaily noteDaily)
    {
        List<NoteDaily> list = noteDailyService.selectNoteDailyList(noteDaily);
        ExcelUtil<NoteDaily> util = new ExcelUtil<NoteDaily>(NoteDaily.class);
        util.exportExcel(response, list, "日常笔记数据");
    }

    /**
     * 获取日常笔记详细信息
     */
    @PreAuthorize("@ss.hasPermi('note:daily:query')")
    @GetMapping(value = "/{noteId}")
    public AjaxResult getInfo(@PathVariable("noteId") Long noteId)
    {
        return success(noteDailyService.selectNoteDailyByNoteId(noteId));
    }

    /**
     * 新增日常笔记
     */
    @PreAuthorize("@ss.hasPermi('note:daily:add')")
    @Log(title = "日常笔记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NoteDaily noteDaily)
    {
        noteDaily.setCreateBy(getUsername());
        return toAjax(noteDailyService.insertNoteDaily(noteDaily));
    }

    /**
     * 修改日常笔记
     */
    @PreAuthorize("@ss.hasPermi('note:daily:edit')")
    @Log(title = "日常笔记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NoteDaily noteDaily)
    {
        try {
            if (noteDaily.getNoteId() == null) {
                return error("笔记ID不能为空");
            }
            noteDaily.setUpdateBy(getUsername());
            int result = noteDailyService.updateNoteDaily(noteDaily);
            logger.info("更新日常笔记，笔记ID: {}, 更新结果: {}", noteDaily.getNoteId(), result);
            return toAjax(result);
        } catch (Exception e) {
            logger.error("修改日常笔记失败", e);
            return AjaxResult.error("修改日常笔记失败: " + e.getMessage());
        }
    }

    /**
     * 删除日常笔记
     */
    @PreAuthorize("@ss.hasPermi('note:daily:remove')")
    @Log(title = "日常笔记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noteIds}")
    public AjaxResult remove(@PathVariable String noteIds)
    {
        return toAjax(noteDailyService.deleteNoteDailyByNoteIds(noteIds));
    }

    /**
     * 增加阅读次数
     */
    @PostMapping("/view/{noteId}")
    public AjaxResult increaseViewCount(@PathVariable("noteId") Long noteId)
    {
        return toAjax(noteDailyService.increaseViewCount(noteId));
    }
}